package com.sig.xml;

import org.xmlpull.v1.XmlPullParser;

import android.util.Log;
import arbre.utils.ArbreBinaire;
import arbre.utils.Noeud;
import arbre.utils.Point;
import arbre.utils.Segment;
import arbre.utils.Verticale;
import arbre.utils.Zone;

public class TreeXmlParser extends XmlParser{

	public TreeXmlParser(String xml) {
		super(xml);
	}

	public TreeXmlParser(XmlPullParser xml){
		super(xml);
	}

	public static String NOEUD="node";
	public static String VERTICALE="verticale";
	public static String SEGMENT="segment";
	public static String POINT="point";
	public static String ZONE="zone";
	private Noeud current;
	public ArbreBinaire getDecisionTree(){
		ArbreBinaire res;

		int eventType = XMLgetEventType(); 
		while (eventType != XmlPullParser.END_DOCUMENT){ 
			if (eventType == XmlPullParser.START_TAG) {
				String tag = getXpp().getName();

				Log.wtf("TreeXML-1",tag);
				if(tag.equals(NOEUD)){
					String dir = getXpp().getAttributeValue(null, "dir");
					
					Log.wtf("dir", dir);
					if(dir.equals("Defaut")){
						
						current=getNoeud(null);
						Log.d("TreeXML-1","default");
					}
				}
				else if(tag.equals(VERTICALE)){

				}
				else if(tag.equals(SEGMENT)){

				}

			}
			eventType=XMLgetSuivant();
		}
		res= new ArbreBinaire();
		res.setRacine(current);

		return res;
	}

	private Noeud getNoeud(Noeud pred) {
		Noeud res = new Noeud(null, null, pred, null,0);
		XMLgetSuivant();
		int eventType = XMLgetEventType();
		while(eventType != XmlPullParser.END_TAG) {  
			if (eventType == XmlPullParser.START_TAG) {
				String tag = getXpp().getName();
				Log.wtf("TreeXML-2",tag);
				if(tag.equals(VERTICALE)){
					Verticale content = getVerticale();
					XMLgetSuivant();
					//res.setContent(content);
					Log.d("TreeXML-2-fin verticale","setcontent verticale");
				}
				else if(tag.equals(NOEUD)){
					String dir = getXpp().getAttributeValue(null, "dir");
					
					Noeud n = getNoeud(res);
					if(dir.equals("left")){
						res.setL(n);
						Log.d("TreeXML-2","setLeft noeud");
					}
					else{
						res.setR(n);
						Log.d("TreeXML-2","setRight noeud");
					}
					//Log.d("node","node found");
				}
				else if (tag.equals(SEGMENT)) {
					Segment content = getSegment();
					Log.d("Segment-2", content.toString());
					//res.setContent(content);
					Log.d("TreeXML-2","setcontent segment");
				}
				else if(tag.equals(ZONE)){
					Zone content = getZone();
					XMLgetSuivant();
					Log.d("Zone", content.toString());
					//res.setContent(content);
					Log.d("TreeXML-2","setcontent zone");
				}
			}
			eventType=XMLgetSuivant();
		}
		Log.d("fin noed", getXpp().getName());
		return res;



	}

	public static final String ZONE_NOM="nom";
	private Zone getZone() {
		Zone res = new Zone();
		String nom = getXpp().getAttributeValue(null, ZONE_NOM);
		res.setNom(nom);
		//XMLgetSuivant();
		return res;
	}


	private Segment getSegment() {
		Segment res = new Segment(null, null);
		int eventType = XMLgetEventType();
		while(eventType != XmlPullParser.END_TAG){
			if (eventType == XmlPullParser.START_TAG) {
				String tag = getXpp().getName();
				Log.wtf("TreeXML-3",tag);
				if(tag.equals(POINT)){
					Point p = getPoint();
					XMLgetSuivant();
					if(res.getDebut() ==null){
						res.setDebut(p);
					}
					else {
						res.setFin(p);
					}

				}
			}
			eventType = XMLgetSuivant();
		}
		return res;
	}


	public static final String POINT_X = "x";
	public static final String POINT_Y = "y";

	private Point getPoint() {
		Point res = new Point(0, 0);
		Float x = Float.parseFloat(getXpp().getAttributeValue(null,POINT_X));
		Float y = Float.parseFloat(getXpp().getAttributeValue(null,POINT_Y));

		res.setX(x);
		res.setY(y);
		
//		getString();
		return res;
	}

	public static final String VERTICALE_ABS="abs";
	private Verticale getVerticale() {
		Verticale res = new Verticale();
		Float abs = Float.parseFloat(getXpp().getAttributeValue(null, VERTICALE_ABS));
		res.setAbs(abs);
//		getString();
		return res;
	}
}
